#!/usr/bin/env python
# -*- coding=utf-8 -*-

# @File    :   test.py
# @Version :   1.0
# @Author  :   xin
# @Time    :   2023/10/23 15:18:19
# Description:
import os
import time
import platform
from HCNetSDK import *
# 设置SDK初始化依赖库路径


def SetSDKInitCfg():
    print("--------SetSDKInitCfg---------")
    global sdk
    # 设置HCNetSDKCom组件库和SSL库加载路径
    osLibPath = b"/home/aoting/testhivsion/test_hikvision/libs"
    sdk_ComPath = NET_DVR_LOCAL_SDK_PATH()
    sdk_ComPath.sPath = osLibPath
    os.chdir(osLibPath)
    sdk = cdll.LoadLibrary(b'./libhcnetsdk.so')
    sdk.NET_DVR_Init()
    # # 启用SDK写日志
    # sdk.NET_DVR_SetLogToFile(3, bytes('./log', encoding='utf-8'), False)
    sdk.NET_DVR_SetSDKInitCfg(2, byref(sdk_ComPath))
    sdk.NET_DVR_SetSDKInitCfg(3,b'./libcrypto.so.1.1')
    sdk.NET_DVR_SetSDKInitCfg(4,b'./libssl.so.1.1')


def main():
    print("--------main2---------")
    # 通用参数配置
    sdkCfg = NET_DVR_LOCAL_GENERAL_CFG()
    sdkCfg.byAlarmJsonPictureSeparate = 1
    sdk.NET_DVR_SetSDKLocalCfg(17, byref(sdkCfg))

    # 初始化用户id, 在调用正常是程序一般返回正数，故初始化一个负数
    UserID = c_long(-1)
    # 用户注册设备
    # c++传递进去的是byte型数据，需要转成byte型传进去，否则会乱码
    # 登录参数，包括设备地址、登录用户、密码等
    struLoginInfo = NET_DVR_USER_LOGIN_INFO()
    struLoginInfo.bUseAsynLogin = 0  # 同步登录方式
    struLoginInfo.sDeviceAddress = bytes("192.168.0.220", "ascii")  # 设备IP地址
    struLoginInfo.wPort = 8000  # 设备服务端口
    struLoginInfo.sUserName = bytes("admin", "ascii")  # 设备登录用户名
    struLoginInfo.sPassword = bytes("Aoting123", "ascii")  # 设备登录密码
    struLoginInfo.byLoginMode = 0
    struDeviceInfoV40 = NET_DVR_DEVICEINFO_V40()
    sdk.NET_DVR_Login_V40.restype = c_long
    sdk.NET_DVR_GetLastError.restype = c_uint
    UserID = sdk.NET_DVR_Login_V40(byref(struLoginInfo), byref(struDeviceInfoV40))
    if UserID < 0:
        print("Login failed, error code: %d" % sdk.NET_DVR_GetLastError())
        sdk.NET_DVR_Cleanup()
    else:
        print('Login success：%s' % str(struDeviceInfoV40.struDeviceV30.sSerialNumber, encoding = "utf8"))

    
    
    
    # 云台控制操作 
    # handle =sdk.NET_DVR_PTZControl_Other(UserID,1,23,1)
    handle =sdk.NET_DVR_PTZControlWithSpeed_Other(UserID,1,23,0,2)
    if handle < 0:
        print("NET_DVR_PTZControl_Other failed, error code: %d" % sdk.NET_DVR_GetLastError())
        sdk.NET_DVR_Logout(UserID)
        sdk.NET_DVR_Cleanup()
    else:
        print('NET_DVR_PTZControl_Other 成功')
    
    while (True):
        pass
    
 


if __name__ == "__main__":
    print("-----main------")
    SetSDKInitCfg()
    main()
    pass
